
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.TooManyListenersException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sip.InvalidArgumentException;
import javax.sip.ObjectInUseException;
import javax.sip.PeerUnavailableException;
import javax.sip.TransportNotSupportedException;

/**
 *
 * @author David Rodriguez A.
 *
 * Referencias:
 * 1) http://download.java.net/communications/jain-sip/nightly/
 * 2) https://jain-sip.dev.java.net/
 * 3) http://www.oracle.com/technology/pub/articles/dev2arch/2007/10/introduction-jain-sip.html#appendix
 *
 */


public class TelematicApp{

    /*public static void main(String argv[]) throws PeerUnavailableException,
                                                  TransportNotSupportedException,
                                                  InvalidArgumentException,
                                                  ObjectInUseException,
                                                  TooManyListenersException,
                                                  UnknownHostException,
                                                  InterruptedException {
*/
    Sip stacksip1;
    Valve valve1, valve2, valve3;
    AppValveViewer appviewer;
    public TelematicApp(AppValveViewer appviewer){
    this.appviewer = appviewer;
        //extrae la ip de la maquina
        String ip = null;
        try {
            ip = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException ex) {
            Logger.getLogger(TelematicApp.class.getName()).log(Level.SEVERE, null, ex);
        }
       // System.out.println("(TeleApp.java) >>> java.net.InetAddress:ip "+ ip);
        //String wlan0 = "190.47.99.166";
        //crea instancia sip con user:PeZ ip: ip

        /*creando valves (Valve(<time>,<id>))*/
        valve1 = new Valve(1,1); //cada 1 segundo se incrementa el level, id:1
        valve2 = new Valve(1,2); //cada 1 segundo se incrementa el level, id:2
        valve3 = new Valve(1,3); //cada 1 segundo se incrementa el level, id:3

        /*pega valvulas 2 y 3 a la valve 1*/
        valve1.attachValve(valve2);
        valve1.attachValve(valve3);
        this.appviewer.setIdvalve(valve1.getId());
        
        valve1.setListener(appviewer); 
        valve1.turnON();
        
        try {
            /*Sip(<username>,<ip>,<port>,<valve>,<stack name>)*/
           stacksip1 = new Sip("pez", ip, 5080, valve1, "ServiceSip_1",appviewer);
        } catch (PeerUnavailableException ex) {
            Logger.getLogger(TelematicApp.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TransportNotSupportedException ex) {
            Logger.getLogger(TelematicApp.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidArgumentException ex) {
            Logger.getLogger(TelematicApp.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ObjectInUseException ex) {
            Logger.getLogger(TelematicApp.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TooManyListenersException ex) {
            Logger.getLogger(TelematicApp.class.getName()).log(Level.SEVERE, null, ex);
        }
       
        
    }



    public Sip getSip(){
        return this.stacksip1;
    }
    public Valve getValve(){
        return this.valve1;
    }

}//end_TeleApp_class
